using System;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Text;
using Android.Widget;
using MuseumsBC.Models;

namespace MuseumsBC.Activities
{
    [Activity(Label = "Museum Details")]
    public class MuseumDetailActivity : Activity
    {
        private Museum _Museum { get; set; }

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Create your application here

            _Museum = setMuseum();

            SetContentView(Resource.Layout.MuseumDetail);

            SetupLabels();

            var btnViewLocation = FindViewById<Button>(Resource.MuseumDetail.btnViewLocation);
            btnViewLocation.Click += new EventHandler(btnViewLocation_Click);
        }

        void btnViewLocation_Click(object sender, EventArgs e)
        {
            var i = new Intent(this, typeof(MuseumDetailMapActivity));
            i.PutExtra("MuseumId", _Museum.Id.ToString()); // must ".ToString()" as later get out as string, not integer
            StartActivity(i);
        }

        private void SetupLabels()
        {
            Android.Widget.RelativeLayout container;
            Android.Widget.LinearLayout body;
            Android.Widget.TextView tv;

            container = FindViewById<RelativeLayout>(Resource.MuseumDetail.container);
            body = container.FindViewById<LinearLayout>(Resource.MuseumDetail.body);

            // TODO: throwing error here. Maybe because now that tvs are in a relative layout,
            // they aren't actually rendered yet, and the findbyid fails?
            tv = body.FindViewById<TextView>(Resource.MuseumDetail.MuseumName);
            tv.Text = FromHtml(_Museum.MuseumName, string.Empty).ToString();

            tv = body.FindViewById<TextView>(Resource.MuseumDetail.Telephone);
            tv.Text = FromHtml(_Museum.Telephone, string.Empty).ToString();

            tv = body.FindViewById<TextView>(Resource.MuseumDetail.Address);
            tv.Text = FromHtml(_Museum.Address, string.Empty).ToString();

            tv = FindViewById<TextView>(Resource.MuseumDetail.Image);
            tv.Text = FromHtml(_Museum.Image, string.Empty).ToString();

            tv = FindViewById<TextView>(Resource.MuseumDetail.Description);
            tv.Text = FromHtml(_Museum.Description, "").ToString();

            tv = FindViewById<TextView>(Resource.MuseumDetail.HoursOfOperation);
            tv.Text = FromHtml(_Museum.HoursOfOperation, string.Empty).ToString();
        }

        private Museum setMuseum()
        {
            int outNum;
            int museumId = int.TryParse(Intent.GetStringExtra("MuseumId"), out outNum) ? outNum : -1;

            return ((MuseumsBCApplication)Application).MuseumRepository.GetMuseumById(museumId);
        }

        /// <summary>
        /// Android.Text.Html.FromHtml method returns null if input value is null. This 
        /// method allows for a defaultValue so that immediate .ToString() doesn't fail
        /// </summary>
        /// <param name="source"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        private static ISpanned FromHtml(string source, string defaultValue)
        {
            if (source == null)
                source = defaultValue;
            if (source == null)
                source = string.Empty;

            return Android.Text.Html.FromHtml(source);
        }
    }
}